// 插入，遍历，深度
export class TreeNode {
  value: number;
  leftChild: TreeNode | null;
  rightChild: TreeNode | null;

  constructor (value: number, leftChild: TreeNode | null = null, rightChild: TreeNode | null = null) {
    this.value = value;
    this.leftChild = leftChild;
    this.rightChild = rightChild;
  }

}

export class Tree {
  root: TreeNode;

  constructor (value: number) {
    this.root = new TreeNode(value);
  }

  insert(value: number) {
    let current: TreeNode | null = this.root;
    while(current) {
      if (value < current.value) {
        if (current.leftChild === null) {
          current.leftChild = new TreeNode(value);
          return;
        }
        else {
          current = current.leftChild;
        }
      }
      else {
        if (current.rightChild === null) {
          current.rightChild = new TreeNode(value);
          return;
        }
        else {
          current = current.rightChild
        }
      }
    }
  }
  // 前序遍历，中左右
  preOrder(node: TreeNode | null = this.root) {
    if (node) {
      console.log(node.value);
      this.preOrder(node.leftChild);
      this.preOrder(node.rightChild);
    }
  }
  // 中序遍历，左中右
  midOrder(node: TreeNode | null = this.root) {
    if (node) {
      this.preOrder(node.leftChild);
      console.log(node.value);
      this.preOrder(node.rightChild);
    }
  }
  // 后序遍历，左右中
  laterOrder(node: TreeNode | null = this.root) {
    if (node) {
      this.preOrder(node.leftChild);
      this.preOrder(node.rightChild);
      console.log(node.value);
    }
  }

  
}
